package com.sgb.kjwl.view.ui.activity.pick;

import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityReportBinding;
import com.sgb.kjwl.di.pick.DaggerPickProjectComponent;
import com.sgb.kjwl.di.pick.PickProjectModule;
import com.sgb.kjwl.model.entity.pick.ReportTender;
import com.sgb.kjwl.view.ui.adapter.self.publish.ReportImageAdapter;
import com.sgb.kjwl.view.widget.pick.PickReportListView;
import com.sgb.kjwl.viewmodel.pick.ReportModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.CfkEditView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class ReportActivity extends AppActivity {

    //行贿受贿
    private static final int TYPE_BRIBERY = 1;
    //服务态度
    private static final int TYPE_SERVICE_STATUS = 2;
    //商业欺诈
    private static final int TYPE_FRAUD = 3;
    //违约纠纷
    private static final int TYPE_DISPUTE = 4;
    //工作作风
    private static final int TYPE_WORK_STYLE = 5;
    //工作偏差
    private static final int TYPE_WORK_DEVIATION = 6;
    //工作偏差
    private static final int TYPE_OTHER = 7;
    //集合
    private List<PickSelectEntity> reportList = new ArrayList<>();

    private int currentType = TYPE_BRIBERY;

    @Inject
    ReportModel viewModel;

    private ActivityReportBinding binding;
    private ReportImageAdapter adapter;
    private List<String> urls;
    private List<File> fileList;
    public int imgTotal;
    private int currentImgNum;
    private List<String> uploadImgList = new ArrayList<>();
    private List<ReportTender.TenderReportingsBean> customFieldListBeans;
    private ReportTender.TenderReportingsBean customFieldListBean;
    private List<ReportTender.FilesBean> fileImageList = new ArrayList<>();
    private String description;
    private String breachPrinciplePeople;
    private String reportingAppeal;
    private String affectConsequence;
    private String suggestedTreatment;
    private String requestScheme;
    private String deviationWork;
    private String reportingPeopleId;
    private String reportingPeopleName;
    private String reportingPeopleTel;
    private String reportingType = "1";
    private int reportingMatters;
    private String reportDetailUrl = "https://user.shigongbang.com/superviseList";
    private String businessNo;
    private String businessType = "0";
    private String processingResults;
    //默认非匿名
    private boolean isAnonymity = false;
    //判断当前是否登录
    private boolean isLogin = false;
    //创建观察者对象，用来观察图片被删除或新增之后数量的变化，用来记录数量
    private ReportImageAdapter.ReportObserver reportObserver = new ReportImageAdapter.ReportObserver() {
        @Override
        public void notifyItemSizeChange(int size) {
            if (size == 0) {
                resetVisibility(binding.tvNum, false);
                return;
            }
            resetVisibility(binding.tvNum, true);
            binding.tvNum.setText(size + " / 9");
        }
    };

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPickProjectComponent.builder().appComponent(appComponent).pickProjectModule(new PickProjectModule(this)).build().inject(this);
    }

    /**
     * @param context
     * @param businessNo   业务编号（招标或者竞价的编号）
     * @param businessType 所属类型（1：招标 2：竞价）
     */
    public static void start(Context context, String businessNo, String businessType) {
        Intent intent = new Intent();
        intent.putExtra("businessNo", businessNo);
        intent.putExtra("businessType", businessType);
        intent.setClass(context, ReportActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        businessNo = getIntent().getStringExtra("businessNo");
        businessType = getIntent().getStringExtra("businessType");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_report);
        binding.setViewModel(viewModel);
        binding.include.titleView.setText("举报监督");
        binding.include.ivBack.setOnClickListener(this);
        binding.ivAddAnsAddField.setOnClickListener(this);
        binding.tvSubmit.setOnClickListener(this);
        binding.tvAnonymity.setOnClickListener(this);
    }

    @Override
    protected void init() {
        initReportType();
        handleClickItem();
    }

    private void initReportType() {
        String[] reportTypeArray = getResources().getStringArray(R.array.report_type);
        for (int i = 0; i < reportTypeArray.length; i++) {
            String reportType = reportTypeArray[i];
            PickSelectEntity temp = new PickSelectEntity();
            temp.setNo(String.valueOf(i + 1));
            temp.setProjectscale(reportType);
            reportList.add(temp);
        }
        ReportTypeAdapter reportTypeAdapter = new ReportTypeAdapter(this, reportList);
        binding.reportList.setLayoutManager(new GridLayoutManager(this, 4));
        binding.reportList.setAdapter(reportTypeAdapter);
        reportTypeAdapter.setItemSelect(true);
        reportTypeAdapter.setDefItem(0);
        reportTypeAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                currentType = Integer.valueOf(pickSelectEntity.getNo());
                handleClickItem();
            }
        });
    }

    private void handleClickItem() {
        reset();
        handleImg();
        handleField();
        handleContactsView();
        switch (currentType) {
            case TYPE_BRIBERY:
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llReportingAppealContainer, true);

                resetData(binding.tvReportingAppeal, "举报诉求");
                resetData(binding.tvDescription, "违纪描述（必填）");
                resetData(binding.tvBreachPrinciplePeople, "违纪人员（必填）");

                setHintText(binding.cfkDescriptionEdit, "请输入违纪描述，最少输入5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入违纪人员姓名");
                setHintText(binding.cfkReportingAppealEdit, "最多输入200字");
                break;
            case TYPE_SERVICE_STATUS:
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llAffectConsequenceContainer, true);
                resetVisibility(binding.llSuggestedTreatmentContainer, true);

                resetData(binding.tvDescription, "过程描述（必填）");
                resetData(binding.tvBreachPrinciplePeople, "相关人员（必填）");
                resetData(binding.tvAffectConsequence, "影响后果");
                resetData(binding.tvRequestScheme, "建议处理");

                setHintText(binding.cfkDescriptionEdit, "请输入过程描述，最少输入5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员姓名");
                setHintText(binding.cfkAffectConsequenceEdit, "最多输入200字");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
            case TYPE_FRAUD:
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llRequestSchemeContainer, true);

                resetData(binding.tvDescription, "内容描述（必填）");
                resetData(binding.tvBreachPrinciplePeople, "相关人员（必填）");
                resetData(binding.tvRequestScheme, "要求方案");

                setHintText(binding.cfkDescriptionEdit, "请输入违约内容描述，最少输入5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员姓名");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
            case TYPE_DISPUTE:
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llRequestSchemeContainer, true);

                resetData(binding.tvDescription, "违约描述（必填）");
                resetData(binding.tvBreachPrinciplePeople, "违约人员（必填）");
                resetData(binding.tvRequestScheme, "要求方案");

                setHintText(binding.cfkDescriptionEdit, "请输入违约描述，最少输入5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入违约人员姓名");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
            case TYPE_WORK_STYLE:
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llAffectConsequenceContainer, true);
                resetVisibility(binding.llSuggestedTreatmentContainer, true);

                resetData(binding.tvDescription, "过程描述（必填）");
                resetData(binding.tvBreachPrinciplePeople, "相关人员（必填）");
                resetData(binding.tvAffectConsequence, "影响后果");
                resetData(binding.tvSuggestedTreatment, "建议处理");

                setHintText(binding.cfkDescriptionEdit, "请输入过程描述，最少输入5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员姓名");
                setHintText(binding.cfkAffectConsequenceEdit, "最多输入200字");
                setHintText(binding.cfkSuggestedTreatmentEdit, "最多输入200字");
                break;
            case TYPE_WORK_DEVIATION:
                resetVisibility(binding.llDeviationWorkContainer, true);
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llSuggestedTreatmentContainer, true);

                resetData(binding.tvDescription, "内容描述（必填）");
                resetData(binding.tvBreachPrinciplePeople, "相关人员");
                resetData(binding.tvSuggestedTreatment, "我的建议");

                setHintText(binding.cfkDescriptionEdit, "请输入内容描述，最少输入5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员姓名");
                setHintText(binding.cfkSuggestedTreatmentEdit, "请输入我的建议最多输入200字");
                break;
            case TYPE_OTHER:
                resetVisibility(binding.llDescriptionContainer, true);
                resetVisibility(binding.llBreachPrinciplePeopleContainer, true);
                resetVisibility(binding.llRequestSchemeContainer, true);

                resetData(binding.tvDescription, "内容描述（必填）");
                resetData(binding.tvBreachPrinciplePeople, "相关人员");
                resetData(binding.tvRequestScheme, "要求方案");

                setHintText(binding.cfkDescriptionEdit, "请输入内容描述，最少输入5个字，最多输入200字");
                setHintText(binding.cfkBreachPrinciplePeopleEdit, "请输入相关人员姓名");
                setHintText(binding.cfkRequestSchemeEdit, "最多输入200字");
                break;
        }
    }

    private void handleContactsView() {
        if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
            isLogin = false;
            resetVisibility(binding.llContactsContainer, true);
            resetVisibility(binding.tvAnonymity, false);
        } else {
            isLogin = true;
            resetVisibility(binding.llContactsContainer, false);
            resetVisibility(binding.tvAnonymity, true);
        }
        isAnonymity = false;
        binding.tvAnonymity.setSelected(isAnonymity);
        binding.itemName.setContent("");
        binding.itemPhone.setContent("");
        binding.itemPhone.setInputType(InputType.TYPE_CLASS_PHONE);
    }

    private void handleImg() {
        urls = new ArrayList<>();
        fileList = new ArrayList<>();
        adapter = new ReportImageAdapter(this, null, null, 9);
        adapter.registerObserver(reportObserver);
        adapter.setDatas(null, urls);
        adapter.setFileList(fileList);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        binding.recyclerView.setAdapter(adapter);
    }

    private void handleField() {
        customFieldListBeans = new ArrayList<>();

        binding.ivAddAnsAddField.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (binding.rootLayout.getChildCount() < 5) {
                    if (binding.rootLayout.getChildCount() > 0) {
                        if (checkItemInput()) {
                            PickReportListView pickReportListView = new PickReportListView(ReportActivity.this);
                            binding.rootLayout.addView(pickReportListView);
                            pickReportListView.setItemChildCLickListener(new PickReportListView.ItemChildCLickListener() {
                                @Override
                                public void deletePos() {
                                    binding.rootLayout.removeView(pickReportListView);
                                }
                            });
                        }
                    } else {
                        PickReportListView pickReportListView = new PickReportListView(ReportActivity.this);
                        binding.rootLayout.addView(pickReportListView);
                        pickReportListView.setItemChildCLickListener(new PickReportListView.ItemChildCLickListener() {
                            @Override
                            public void deletePos() {
                                binding.rootLayout.removeView(pickReportListView);
                            }
                        });
                    }

                } else {
                    MToast.showLongToast(getApplicationContext(), "最多添加5条");
                }
            }
        });
    }

    private boolean checkItemInput() {
        for (int i = 0; i < binding.rootLayout.getChildCount(); i++) {
            PickReportListView pickReportListView = (PickReportListView) binding.rootLayout.getChildAt(i);
            if (pickReportListView.getViewContent()) {
                MToast.showLongToast(getApplicationContext(), "有新增的标题和内容未填写");
                return false;
            }
        }
        return true;
    }

    private void reset() {
        resetVisibility(binding.llDescriptionContainer, false);
        resetVisibility(binding.llBreachPrinciplePeopleContainer, false);
        resetVisibility(binding.llReportingAppealContainer, false);
        resetVisibility(binding.llAffectConsequenceContainer, false);
        resetVisibility(binding.llSuggestedTreatmentContainer, false);
        resetVisibility(binding.llRequestSchemeContainer, false);
        resetVisibility(binding.llDeviationWorkContainer, false);
        resetVisibility(binding.tvNum, false);

        resetData(binding.cfkDeviationWorkEdit, "");
        resetData(binding.cfkAffectConsequenceEdit, "");
        resetData(binding.cfkBreachPrinciplePeopleEdit, "");
        resetData(binding.cfkDescriptionEdit, "");
        resetData(binding.cfkReportingAppealEdit, "");
        resetData(binding.cfkRequestSchemeEdit, "");
        resetData(binding.cfkSuggestedTreatmentEdit, "");
        resetData(binding.tvNum, "");

        resetData();

    }

    private void resetData() {
        description = null;
        breachPrinciplePeople = null;
        reportingAppeal = null;
        affectConsequence = null;
        suggestedTreatment = null;
        requestScheme = null;
        deviationWork = null;
        reportingPeopleId = null;
        reportingPeopleName = null;
        reportingPeopleTel = null;
        reportingType = "1";
        reportingMatters = 0;
        reportDetailUrl = null;
        processingResults = null;

    }

    private void resetVisibility(View view, boolean isShow) {
        if (view != null) {
            view.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    private void resetData(TextView view, String text) {
        if (view != null) {
            view.setText(text);
        }
    }

    private void setHintText(CfkEditView view, String text) {
        CfkEditView.setHintText(view, text);
    }

    private void resetData(CfkEditView view, String text) {
        if (view != null) {
            view.setEditContent(text);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_anonymity:
                binding.tvAnonymity.setSelected(isAnonymity = !isAnonymity);
                break;
            case R.id.tv_submit:
                resetData();
                if (isCanSubmit()) {
                    reportingMatters = currentType;
                    reportingType = isAnonymity ? "2" : "1";

                    if (adapter.getFileList().size() != 0) {
                        imgTotal = adapter.getFileList().size();
                        viewModel.uploadMutiFileModel(adapter.getFileList().get(currentImgNum), 0, currentImgNum + 1);
                    } else {
                        uploadImgSuccess(adapter.getUrls(), 1);
                    }
                }
                break;
        }
    }

    private boolean isCanSubmit() {
        if (currentType == TYPE_BRIBERY) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入违纪描述，最少输入5个字，最多输入200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入违纪人员姓名");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            reportingAppeal = binding.cfkReportingAppealEdit.getEditContent();
        } else if (currentType == TYPE_SERVICE_STATUS) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入过程描述，最少输入5个字，最多输入200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员姓名");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            affectConsequence = binding.cfkAffectConsequenceEdit.getEditContent();
            requestScheme = binding.cfkRequestSchemeEdit.getEditContent();
        } else if (currentType == TYPE_FRAUD) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入违约内容描述，最少输入5个字，最多输入200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员姓名");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            requestScheme = binding.cfkRequestSchemeEdit.getEditContent();
        } else if (currentType == TYPE_DISPUTE) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入违约描述，最少输入5个字，最多输入200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入违约人员姓名");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            requestScheme = binding.cfkRequestSchemeEdit.getEditContent();
        } else if (currentType == TYPE_WORK_STYLE) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入过程描述，最少输入5个字，最多输入200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员姓名");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            affectConsequence = binding.cfkAffectConsequenceEdit.getEditContent();
            suggestedTreatment = binding.cfkSuggestedTreatmentEdit.getEditContent();
        } else if (currentType == TYPE_WORK_DEVIATION) {
            if (TextUtils.isEmpty(binding.cfkDeviationWorkEdit.getEditContent()) || binding.cfkDeviationWorkEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入走偏工作，最少输入5个字，最多输入200字");
                return false;
            }
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入内容描述，最少输入5个字，最多输入200字");
                return false;
            }

            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员姓名");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            deviationWork = binding.cfkDeviationWorkEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            suggestedTreatment = binding.cfkSuggestedTreatmentEdit.getEditContent();
        } else if (currentType == TYPE_OTHER) {
            if (TextUtils.isEmpty(binding.cfkDescriptionEdit.getEditContent()) || binding.cfkDescriptionEdit.getEditContent().length() < 5) {
                MToast.showToast(getApplicationContext(), "请输入内容描述，最少输入5个字，最多输入200字");
                return false;
            }

            if (TextUtils.isEmpty(binding.cfkBreachPrinciplePeopleEdit.getEditContent())) {
                MToast.showToast(getApplicationContext(), "请输入相关人员姓名");
                return false;
            }
            description = binding.cfkDescriptionEdit.getEditContent();
            breachPrinciplePeople = binding.cfkBreachPrinciplePeopleEdit.getEditContent();
            requestScheme = binding.cfkRequestSchemeEdit.getEditContent();
        }
        if (!isLogin) {
            if (!TextUtils.isEmpty(binding.itemPhone.getContent())){
                if (!CheckUtils.isPhoneNumberValid(binding.itemPhone.getContent())) {
                    Toast.makeText(this, "请填写正确的联系电话", Toast.LENGTH_SHORT).show();
                    return false;
                }
            }
            reportingPeopleName = binding.itemName.getContent();
            reportingPeopleTel = binding.itemPhone.getContent();
        } else {
            reportingPeopleId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID, "");
        }
        if (!checkItemInput()) {
            return false;
        }
        return true;
    }

    public ReportTender initTender() {
        ReportTender tender = new ReportTender();
        tender.setAffectConsequence(affectConsequence);
        tender.setDescription(description);
        tender.setBreachPrinciplePeople(breachPrinciplePeople);
        tender.setReportingAppeal(reportingAppeal);
        tender.setSuggestedTreatment(suggestedTreatment);
        tender.setRequestScheme(requestScheme);
        tender.setDeviationWork(deviationWork);
        tender.setReportingPeopleId(reportingPeopleId);
        tender.setReportingPeopleName(reportingPeopleName);
        tender.setReportingPeopleTel(reportingPeopleTel);
        tender.setReportingType(reportingType);
        tender.setReportingMatters(reportingMatters);
        tender.setReportDetailUrl(reportDetailUrl);
        tender.setBusinessNo(businessNo);
        tender.setBusinessType(Integer.valueOf(businessType));
        tender.setFiles(fileImageList);
        tender.setProcessingResults(processingResults);
        tender.setTenderReportings(customFieldListBeans);
        return tender;
    }

    private void uploadImgSuccess(List<String> urls, int type) {
        if (type == 0) {
            //拼接图片url
            for (int i = 0; i < urls.size(); i++) {
                ReportTender.FilesBean fileListBean = new ReportTender.FilesBean();
                int picI = i + 1;
                fileListBean.setName("第" + picI + "张图片");
                fileListBean.setFileUrl(urls.get(i));
                fileImageList.add(fileListBean);
            }
        }
        for (int i = 0; i < binding.rootLayout.getChildCount(); i++) {
            PickReportListView pickReportListView = (PickReportListView) binding.rootLayout.getChildAt(i);
            customFieldListBeans.add(pickReportListView.getContent());
        }
        viewModel.reportAdd(initTender());
    }


    //图片单张上传
    public void uploadRefresh(String url, int type) {
        uploadImgList.add(url);
        if (currentImgNum + 1 == imgTotal) {
            uploadImgSuccess(uploadImgList, type);
        } else {
            currentImgNum++;
            viewModel.uploadMutiFileModel(adapter.getFileList().get(currentImgNum), 0, currentImgNum + 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                fileList.clear();
                urls.clear();
                for (LocalMedia localMedia : selectList) {
                    File file = new File(localMedia.getRealPath());
                    fileList.add(file);
                    urls.add(localMedia.getRealPath());
                }
                adapter.setDatas(selectList, urls);
                adapter.setFileList(fileList);
            }
        }
    }

    @Override
    public void finish() {
        if (getCurrentFocus() != null) {
            ((InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
        super.finish();
    }

    @Override
    protected void onDestroy() {
        if (adapter != null) {
            adapter.unRegisterObserver(reportObserver);
        }
        super.onDestroy();
    }



    public class ReportTypeAdapter<T> extends BaseRecycleAdapter<T> {

        public ReportTypeAdapter(Context context, List<T> datas) {
            super(context, R.layout.item_report_type, datas);
        }

        @Override
        public void convert(ItemViewHolder holder, T s) {
            holder.setBinding(BR.viewModel, s);
        }
    }

}
